package com.data_structure_algorithm.algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * @Authror PABLO
 * @Date 2022/4/4 21:47
 * @Desc 线性查找
 */
public class LineFind {
    public static void main(String[] args) {
        int[] arr = new int[100];
        for (int i = 0; i < 100; i++) {
                arr[i] = i;
        }

        System.out.println(Arrays.toString(arr));
        List<Integer> list = twoFind(arr, 0, arr.length - 1, 1);
         System.out.println(list.toString());

        /*List<Integer> list = find(arr, 33);
        System.out.println(list.toString());*/

    }

    public static List<Integer> find(int[] arr, int value) {

        List<Integer> list = new ArrayList<>();
        if (arr.length == 0) {
            return null;
        }

        for (int i = 0; i < arr.length; i++) {


            if (value == arr[i]){
                //找到该值，继续往后找
                list.add(i);
            }


        }

        return list;
    }

    public static List<Integer> twoFind(int[] arr, int left, int right, int value) {


        List<Integer> list = new ArrayList<>();

        if (arr.length == 0 || value < arr[0] || value > arr.length -1) {
            return null;
        }

        if (left <= right) {
            int mid = left + (right - left) * (value - arr[left]) / (arr[right] - arr[left]);
            System.out.println(mid);
            if (arr[mid] == value) {

                //左扫描+向右扫描
                int temp = mid - 1;
                while (temp > 0 && value == arr[temp]) {
                    list.add(temp);
                    temp--;
                }
                list.add(mid);
                //指定临时下标位置
                temp = mid + 1;
                while (temp < right && value == arr[temp]) {
                    list.add(temp);
                    temp++;
                }
                return list;
            }

            if (arr[mid] < value) {//右边

                return twoFind(arr, mid + 1, right, value);
            } else {
                return twoFind(arr, left, mid - 1, value);
            }


        }


        return list;
    }
}
